package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.CommonTable;
import  com.ifp.weixin.MessageMng.Message.resp.RespArticleInfoDefine;
import com.ifp.weixin.MessageMng.Message.resp.RespMsgInfoDefine;
import ywpt.model.UnitInfo;
import ywpt.model.WeixinAccount;
import ywpt.web.common.PublicFunction;
@Service("articleService")
@RemoteProxy(name="articleService")
public class ArticleServiceImpl extends HibernateEntityDao<RespArticleInfoDefine> implements ArticleService {

	/* （非 Javadoc）
	 * @see ywpt.service.config.ArticleService#getArticleByTitle(java.lang.String, java.lang.String, java.lang.Long, int, int)
	 */
	public List<RespArticleInfoDefine> getArticleByTitle(String title, String name, Long unit_id, int start, int size) {
		// TODO 自动生成方法存根
		DetachedCriteria query = DetachedCriteria.forClass(RespArticleInfoDefine.class);
		query.createAlias("weixinAccount", "weixinAccount").add(Restrictions.eq("weixinAccount.unitInfo.id", unit_id));
		
		if ( name != null && !name.trim().equals("") )
		{
			query.createAlias("weixinAccount", "weixinAccount").add(Restrictions.like("weixinAccount.name", "%" + name + "%"));
		}
		if ( title != null && !title.trim().equals("") )
		{
			query.add(Restrictions.like("title", "%" + title + "%"));
		}		
		query.addOrder(Order.asc("id"));
		
		List<RespArticleInfoDefine> artListList = new ArrayList<RespArticleInfoDefine>();
		
		List<RespArticleInfoDefine> artListLists = new ArrayList<RespArticleInfoDefine>();
		
		RespArticleInfoDefine articleInfoDefine =new RespArticleInfoDefine();
		
		artListList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<RespArticleInfoDefine> itr = artListList.iterator(); itr.hasNext(); i++ ) 
		{
			articleInfoDefine = itr.next();
			String ss = PublicFunction.format(articleInfoDefine.getTitle() + "",25, "  ", 1); 
			
			articleInfoDefine.setTitle(ss);
			artListLists.add(articleInfoDefine);
		}
		return artListLists;
	}

	/* （非 Javadoc）
	 * @see ywpt.service.config.ArticleService#getArticleCountByCriteria(org.hibernate.criterion.DetachedCriteria)
	 */
	public int getArticleCountByCriteria(DetachedCriteria query) {
		// TODO 自动生成方法存根
		return this.getCountByCriteria(query);
	}

	/* （非 Javadoc）
	 * @see ywpt.service.config.ArticleService#getArticleCountByName(java.lang.String, java.lang.String, java.lang.Long)
	 */
	public int getArticleCountByName(String title, String name, Long unit_id) {
		// TODO 自动生成方法存根
		DetachedCriteria query = DetachedCriteria.forClass(RespArticleInfoDefine.class);
		query.createAlias("weixinAccount", "weixinAccount").add(Restrictions.eq("weixinAccount.unitInfo.id", unit_id));
		
		if ( name != null && !name.trim().equals("") )
		{
			query.createAlias("weixinAccount", "weixinAccount").add(Restrictions.like("weixinAccount.name", "%" + name + "%"));
		}
		if ( title != null && !title.trim().equals("") )
		{
			query.add(Restrictions.like("title", "%" + title + "%"));
		}	
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	/* （非 Javadoc）
	 * @see ywpt.service.config.ArticleService#getArticleInfos(org.hibernate.criterion.DetachedCriteria, int, int)
	 */
	public List<RespArticleInfoDefine> getArticleInfos(DetachedCriteria query, int pageSize, int startIndex) {
		// TODO 自动生成方法存根
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	/* （非 Javadoc）
	 * @see ywpt.service.config.ArticleService#getWeixinAccountById(java.lang.Long)
	 */
	public RespArticleInfoDefine getArticleById(Long id) {
		// TODO 自动生成方法存根
		return this.get(id);
	}

	/* （非 Javadoc）
	 * @see ywpt.service.config.ArticleService#removeArticleInfo(java.lang.Long)
	 */
	public void removeArticleInfo(Long id) {
		// TODO 自动生成方法存根
		RespArticleInfoDefine respArticleInfoDefine = this.get(id);
		respArticleInfoDefine.setDelete_Flag(false);
		this.update(respArticleInfoDefine);
	}

	/* （非 Javadoc）
	 * @see ywpt.service.config.ArticleService#saveArticleInfo(ywpt.model.RespArticleInfoDefine, java.lang.Long, java.lang.Long)
	 */
	public void saveArticleInfo(RespArticleInfoDefine articleInfoDefine, Long respMsgInfoDefineId, Long weixinAccountId) {
		// TODO 自动生成方法存根
		if(respMsgInfoDefineId!=null)
			articleInfoDefine.setRespMsgInfoDefine(this.get(RespMsgInfoDefine.class, respMsgInfoDefineId));
		if(weixinAccountId!=null)
			articleInfoDefine.setWeixinAccount(this.get(WeixinAccount.class, weixinAccountId));
		super.save(articleInfoDefine);
	}

	/* （非 Javadoc）
	 * @see ywpt.service.config.ArticleService#saveArticleInfo(ywpt.model.RespArticleInfoDefine)
	 */
	public void saveArticleInfo(RespArticleInfoDefine articleInfoDefine) {
		// TODO 自动生成方法存根
		super.save(articleInfoDefine);
	}

	/* （非 Javadoc）
	 * @see ywpt.service.config.ArticleService#updateArticleInfo(ywpt.model.RespArticleInfoDefine, java.lang.Long, java.lang.Long)
	 */
	public void updateArticleInfo(RespArticleInfoDefine articleInfoDefine, Long respMsgInfoDefineId, Long weixinAccountId) {
		// TODO 自动生成方法存根
		if(respMsgInfoDefineId!=null)
			articleInfoDefine.setRespMsgInfoDefine(this.get(RespMsgInfoDefine.class, respMsgInfoDefineId));
		
		if(weixinAccountId!=null)
			articleInfoDefine.setWeixinAccount(this.get(WeixinAccount.class, weixinAccountId));
		this.update(articleInfoDefine);
	}

	public List<RespArticleInfoDefine> getArticleInfos() {
		// TODO 自动生成方法存根
		return this.getAll();
	}
}
